package com.yanqu.road.utils;

import java.io.PrintWriter;
import java.io.StringWriter;

public class StackHelper {

    public static String getErrorStackInfo(Throwable error) {

        StringBuilder result = new StringBuilder();
        if (error != null) {
            result.append("***************************************************************************\n");

            StringWriter writer = new StringWriter();
            PrintWriter print = new PrintWriter(writer);
            error.printStackTrace(print);
            result.append(writer.toString());

            result.append("***************************************************************************");
        } else {
            result.append("no exception!");
        }
        return result.toString();
    }

    public static String printMyStatckTrace() {
        Throwable throwable = new Throwable();
        StackTraceElement[] stackElements = throwable.getStackTrace();
        StringBuilder sb = new StringBuilder();
        if (null != stackElements) {
            for (int i = 0; i < stackElements.length; i++) {
                sb.append(stackElements[i].getClassName());
                sb.append(".").append(stackElements[i].getMethodName());
                sb.append("(").append(stackElements[i].getFileName()).append(":");
                sb.append(stackElements[i].getLineNumber()+")").append("\n");
            }
        }
        return sb.toString();
    }
}
